package com.ruoyi.asset.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.ruoyi.common.config.RuoYiConfig;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

public class QRcodeUtil {
    private static final int QR_CODE_SIZE = 200;

    public static String createImg(String imgName) throws Exception {
        File file = new File(RuoYiConfig.getDownloadPath()+"qrcode/");
        if(!file.exists() && !file.isDirectory()){
            file.mkdirs();
        }
        Path filePath = Paths.get(RuoYiConfig.getDownloadPath()+"qrcode/"+imgName+".png");
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(imgName, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE, getQRCodeHints());
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);
        return "profile/download/qrcode/"+imgName+".png";
    }

    private static java.util.HashMap<EncodeHintType, Object> getQRCodeHints() {
        java.util.HashMap<EncodeHintType, Object> hints = new java.util.HashMap<>();
        hints.put(EncodeHintType.MARGIN, 1); // 默认边距2
        return hints;
    }

    public static String suijiNumber(){
        String returnStr = System.currentTimeMillis()+""+new Random().nextInt(1000);
        return returnStr;
    }

    public static void main(String[] ars){
        double d1 = 2240;
        System.out.println(d1/10000);

    }


}
